<template>
	<view class="body-view">
		<!-- 使用scroll-view实现tabs滑动切换 -->
		<scroll-view class="top-menu-view" :style="topMenuViewStyle" scroll-x scroll-with-animation
			:scroll-left="scrollLeft">
			<view class="menu-topic-view" v-for="(item,index) in tabs" :id="'tabNum'+item.id" :key="index"
				@click="swichMenu(index)">
				<view :class="currentTab==index ? 'menu-topic-act' : 'menu-topic'">
					<text class="menu-topic-text" :style="txtStyle">{{item.name}}</text>
					<view class="menu-topic-bottom">
						<view class="menu-topic-bottom-color" :style="currentTab==index && lineStyle"></view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		props: {
			index: {
				type: Number,
				default: 1
			},
			topMenuViewStyle: {
				type: Object,
				default: () => {
					return {}
				}
			},
			txtStyle: {
				type: String,
				default: ""
			},
			lineStyle: {
				type: String,
				default: ""
			},
			tabs: {
				type: Array,
				default: () => {
					return [{
							id: 1,
							name: '选项卡一'
						},
						{
							id: 2,
							name: '选项卡二'
						},
						{
							id: 3,
							name: '选项卡三'
						},
						{
							id: 4,
							name: '选项卡四'
						},
						{
							id: 5,
							name: '选项卡五'
						},
						{
							id: 6,
							name: '选项卡六'
						},
						{
							id: 7,
							name: '选项卡七'
						},
					]
				}
			}
		},
		watch: {
			index: {
				handler: function(val) {
					this.currentTab = val
					this.handleComputeLeft(val)
				},
				immediate: true
			}
		},
		data() {
			return {
				scrollLeft: 0,
				currentTab: 0
			}
		},
		onLoad() {

		},
		methods: {
			swichMenu(index) {
				this.currentTab = index
				// 滑动swiper后，每个选项距离其父元素最左侧的距离
				this.scrollLeft = 0;
				this.handleComputeLeft(index)
				this.$emit("tabChange", {
					index
				})
			},
			handleComputeLeft(index) {
				if (index <= 2) {
					this.scrollLeft = 0
					return
				}
				for (let i = 0; i < index; i++) {
					this.scrollLeft += 60
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.body-view {
		width: 100%;
		overflow: hidden;
	}

	.top-menu-view {
		display: flex;
		position: fixed;
		z-index: 100;
		//top: 84upx;
		/* #ifdef H5 */
		top: 84upx;
		/* #endif */
		/* #ifndef H5 */
		top: 0upx;
		/* #endif */
		left: 0;
		white-space: nowrap;
		width: 100%;
		height: 86upx;
		line-height: 86upx;
		border-top: 1upx solid #d8dbe6;

		.menu-topic-view {
			display: inline-block;
			white-space: nowrap;
			height: 86upx;
			position: relative;

			.menu-topic-text {
				padding: 0 21upx;
				height: 44upx;
				line-height: 38upx;
				font-size: 32upx;
				font-family: PingFang SC-Medium, PingFang SC;
				font-weight: 500;
				color: #666666;
			}

			.menu-topic-bottom {
				position: absolute;
				bottom: 0;
				width: 100%;

				.menu-topic-bottom-color {
					width: 48upx;
					height: 6upx;
				}
			}

			.menu-topic-act .menu-topic-bottom {
				display: flex;
				justify-content: center;
			}

			.menu-topic-act .menu-topic-text {
				height: 50upx;
				line-height: 42upx;
				font-size: 36upx;
				font-family: PingFang SC-Bold, PingFang SC;
				font-weight: bold;
				color: #000000;
			}


			.menu-topic-act .menu-topic-bottom-color {
				background: #000000;
			}
		}
	}


	/* 隐藏滚动条，但依旧具备可以滚动的功能 */
	/deep/.uni-scroll-view::-webkit-scrollbar {
		display: none
	}
</style>